package com.NewtifyMe;

import java.util.ArrayList;
import java.util.Iterator;

//TODO add a function that opens a url when a title is clicked.

public class SearchItem {
	private String keyworkd;
	private ArrayList<URLItem> urlList;
	
	public SearchItem(String keyword){
		this.keyworkd = keyword;
		this.urlList = new ArrayList<URLItem>();
	}
	
	public void addUrl(String title, String url){
		URLItem temp = new URLItem(title,url);
		urlList.add(temp);
	}
	
	public String[] getTitleList(){
		ArrayList<String> list= new ArrayList<String>();
		
		Iterator<URLItem> it = urlList.iterator();
		while(it.hasNext()){
			list.add(it.next().getTitle());
		}
		String ret[] = new String[list.toArray().length];
		
		for (int i=0; i<list.toArray().length;i++){
			ret[i] = (String)list.toArray()[i];
		}
		
		return ret;
	}
	
	public boolean hasWatched(){
		Iterator<URLItem> it = urlList.iterator();
		while(it.hasNext()){
			if (it.next().isNew())
				return true;
		}
		return false;
	}
	public String getUrl(String title){
		Iterator<URLItem> it = urlList.iterator();
		while(it.hasNext()){
			String url = it.next().getUrl();
		
			if (url.equals(title))
				return url;
		}
		return null;
	}
	
	public void setWatch(String title){
		Iterator<URLItem> it = urlList.iterator();
		while(it.hasNext()){
			URLItem temp = it.next();
			if (temp.title.equals(title)){
				temp.setNew(true);
			}
		}
	}

	
	public void removeUrl(String title){
		Iterator<URLItem> it = urlList.iterator();
		while(it.hasNext()){
			URLItem temp = it.next();
			if (temp.title.equals(title)){
				urlList.remove(temp);
			}
		}
	}
	
	
	public ArrayList<URLItem> getUrlList() {
		return urlList;
	}

	public void setUrlList(ArrayList<URLItem> urlList) {
		this.urlList = urlList;
	}

	public String getKeyworkd() {
		return keyworkd;
	}

	public void setKeyworkd(String keyworkd) {
		this.keyworkd = keyworkd;
	}



	private class URLItem{
		private String url;
		private String title;
		private boolean isNew = false;

		public URLItem(String title, String url){
			this.url= url;
			this.title = title;
			isNew = false;
		}
		
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public boolean isNew() {
			return isNew;
		}

		public void setNew(boolean isNew) {
			this.isNew = isNew;
		}
		
	}
}
